package com.hgkj.cloud.customer.security;

import com.hgkj.cloud.base.domain.AdminUser;
import com.hgkj.cloud.base.domain.EUser;
import com.hgkj.cloud.base.service.AdminUserService;
import com.hgkj.cloud.base.service.EUserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.SimpleByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

public class HCRealm extends AuthorizingRealm {
    private static Log log = LogFactory.getLog(HCRealm.class);
    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    private EUserService eUserService;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
        String shost = token.getHost();
        log.info("============================================ 认证");
        log.info("============================================ token:" + token);
        log.info("============================================ tokenhost:" + shost);

        AdminUser mAdminUser = null;
        EUser user= null;
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            if ("AdminUserLogin".equals(shost)) {
                try {
                    Map<String, Object> mapTemp = new HashMap<>();
                    mapTemp.put("user_name", token.getUsername());
                    mAdminUser = adminUserService.getAdminUserByMap(mapTemp);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                String host = token.getHost();
                if("uuid".equals(host)) {
                    map.put("uuid", token.getUsername().toString());
                } else {
                    map.put("channel_id", token.getUsername().substring(token.getUsername().lastIndexOf("_") + 1));
                    map.put("user_name", token.getUsername().substring(0, token.getUsername().lastIndexOf("_")));
                }
                user = eUserService.getExamineUserInfo(map);
//                String suserpwd=user.getUser_pwd();
//                String susername=user.getUser_name();
//                user.setUser_pwd(new Md5Hash("123456", susername).toHex());
                if("uuid".equals(host)){
                    user.setUser_pwd(new Md5Hash(token.getPassword(), user.getUser_name().trim()).toHex());
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        if ("AdminUserLogin".equals(shost)) {
            if (mAdminUser != null) {
                return new SimpleAuthenticationInfo(mAdminUser, mAdminUser.getUser_pwd(), new SimpleByteSource(mAdminUser.getUser_name()), getName());
            } else {
                return null;
            }
        } else {
            if (user != null) {
                return new SimpleAuthenticationInfo(user, user.getUser_pwd(), new SimpleByteSource(user.getUser_name()), getName());
            } else {
                return null;
            }
        }
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        //根据自己系统规则的需要编写获取授权信息，这里为了快速入门只获取了用户对应角色的资源url信息
        String username = (String) principals.fromRealm(getName()).iterator().next();
        log.debug("============================================ 授权");
        log.debug("============================================ username:" + username);
        if (username != null) {
            /*List<String> pers = userDao.getPermissionsByUserName(username);
            if (pers != null && !pers.isEmpty()) {
                SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
                for (String each : pers) {
                    //将权限资源添加到用户信息中
                    info.addStringPermission(each);
                }
                return info;
            }  */
        }
        return null;
    }
}
